package IO流复习;

import java.io.*;
import java.util.Scanner;

public class BufferedInput_outputStream_Text {
    public static void main(String[] args) {
        /*BufferedInputStream bf= null;
        BufferedOutputStream bo= null;
        try {
            bf = new BufferedInputStream(new FileInputStream(""));
            bo = new BufferedOutputStream(new FileOutputStream(""));
            byte[] bytes=new byte[1024];
            int len;
            while ((len=bf.read(bytes))!=-1){
                bo.write(bytes,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭资源
            try {
                if (bo!=null)
                bo.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if (bf!=null)
                bf.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }*/
        //测试缓冲流和普通流的拷贝速度
        long l = System.currentTimeMillis();
        bufferedOut("D:\\idea2022\\day.com.qiezi\\day_13\\src\\IO流复习\\a.txt","D:\\idea2022\\day.com.qiezi\\day_13\\src\\IO流复习\\a1.txt");
        long end = System.currentTimeMillis();
        System.out.println("使用的时间:"+(end-l));

    }
    public static void bufferedIn(String src,String dest){
        BufferedInputStream bf= null;
        BufferedOutputStream bo= null;
        try {
            bf = new BufferedInputStream(new FileInputStream(src));
            bo = new BufferedOutputStream(new FileOutputStream(dest));
            byte[] bytes=new byte[1024];
            int len;
            while ((len=bf.read(bytes))!=-1){
                bo.write(bytes,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭资源
            try {
                if (bo!=null)
                    bo.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if (bf!=null)
                    bf.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public static void bufferedOut(String src,String dest){
        FileInputStream fi= null;
        FileOutputStream fo= null;
        try {
            fi = new FileInputStream(src);
            fo = new FileOutputStream(dest);
            byte [] bytes=new byte[50];
            int len;
            while ((len=fi.read(bytes))!=-1){
                fo.write(bytes,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (fo!=null)
                fo.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if (fi!=null)
                fi.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
